package cloud.lysky.order;

import com.alibaba.cloud.nacos.NacosConfigManager;
import com.alibaba.nacos.api.config.listener.Listener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

@Slf4j
@EnableDiscoveryClient
@SpringBootApplication
@EnableFeignClients // 开启Feign的功能
public class OrderApplication {

    public static void main(String[] args) {
        SpringApplication.run(OrderApplication.class, args);
    }

    //1.项目启动就监听配置文件的变化
    //2.发生变化后拿到变化值
    //3.发送邮件通知

    @Bean
    ApplicationRunner applicationRunner(NacosConfigManager nacosConfigManager) {
        return args -> {
            nacosConfigManager.getConfigService().addListener("service-order.properties", "DEFAULT_GROUP", new Listener() {
                @Override
                public Executor getExecutor() {
                    return Executors.newFixedThreadPool(3);
                }

                @Override
                public void receiveConfigInfo(String s) {
                    log.info("Receive change config info: {}", s);
                    // 执行邮件发送的代码(省略)
                }
            });
        };
    }
}
